//package health_system.health_system.exception;
//
//import jakarta.validation.ConstraintViolationException;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.ResponseEntity;
//import org.springframework.security.access.AccessDeniedException;
//import org.springframework.security.authentication.BadCredentialsException;
//import org.springframework.validation.BindException;
//import org.springframework.validation.FieldError;
//import org.springframework.web.bind.MethodArgumentNotValidException;
//import org.springframework.web.bind.annotation.ExceptionHandler;
//import org.springframework.web.bind.annotation.ResponseStatus;
//import org.springframework.web.bind.annotation.RestControllerAdvice;
//import org.springframework.web.context.request.WebRequest;
//
//import java.util.HashMap;
//import java.util.Map;
//import java.util.stream.Collectors;
//
//@Slf4j
//@RestControllerAdvice
//public class GlobalExceptionHandler {
//
//    @ExceptionHandler(ResourceNotFoundException.class)
//    @ResponseStatus(HttpStatus.NOT_FOUND)
//    public ResponseEntity<ErrorResponse> handleResourceNotFoundException(ResourceNotFoundException ex, WebRequest request) {
//        log.error("Resource not found exception occurred: {}", ex.getMessage());
//        return createErrorResponse(HttpStatus.NOT_FOUND, ex.getMessage(), request.getDescription(false));
//    }
//
//    @ExceptionHandler(PatientNotFoundException.class)
//    @ResponseStatus(HttpStatus.NOT_FOUND)
//    public ResponseEntity<ErrorResponse> handlePatientNotFoundException(PatientNotFoundException ex, WebRequest request) {
//        log.error("Patient not found exception occurred: {}", ex.getMessage());
//        return createErrorResponse(HttpStatus.NOT_FOUND, ex.getMessage(), request.getDescription(false));
//    }
//
//    @ExceptionHandler(UserNotFoundException.class)
//    @ResponseStatus(HttpStatus.NOT_FOUND)
//    public ResponseEntity<ErrorResponse> handleUserNotFoundException(UserNotFoundException ex, WebRequest request) {
//        log.error("User not found exception occurred: {}", ex.getMessage());
//        return createErrorResponse(HttpStatus.NOT_FOUND, ex.getMessage(), request.getDescription(false));
//    }
//
//    @ExceptionHandler(BadCredentialsException.class)
//    @ResponseStatus(HttpStatus.UNAUTHORIZED)
//    public ResponseEntity<ErrorResponse> handleBadCredentialsException(BadCredentialsException ex, WebRequest request) {
//        log.error("Bad credentials exception occurred");
//        return createErrorResponse(HttpStatus.UNAUTHORIZED, "Invalid username or password", request.getDescription(false));
//    }
//
//    @ExceptionHandler(AccessDeniedException.class)
//    @ResponseStatus(HttpStatus.FORBIDDEN)
//    public ResponseEntity<ErrorResponse> handleAccessDeniedException(AccessDeniedException ex, WebRequest request) {
//        log.error("Access denied exception occurred: {}", ex.getMessage());
//        return createErrorResponse(HttpStatus.FORBIDDEN, "Access denied", request.getDescription(false));
//    }
//
//    @ExceptionHandler(MethodArgumentNotValidException.class)
//    @ResponseStatus(HttpStatus.BAD_REQUEST)
//    public ResponseEntity<ErrorResponse> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex, WebRequest request) {
//        Map<String, String> errors = new HashMap<>();
//        ex.getBindingResult().getAllErrors().forEach(error -> {
//            String fieldName = ((FieldError) error).getField();
//            String errorMessage = error.getDefaultMessage();
//            errors.put(fieldName, errorMessage);
//        });
//
//        String errorMessage = errors.entrySet().stream()
//                .map(entry -> entry.getKey() + ": " + entry.getValue())
//                .collect(Collectors.joining(", "));
//
//        log.error("Validation error: {}", errorMessage);
//        return createErrorResponse(HttpStatus.BAD_REQUEST, "Validation error", request.getDescription(false), errors);
//    }
//
//    @ExceptionHandler(BindException.class)
//    @ResponseStatus(HttpStatus.BAD_REQUEST)
//    public ResponseEntity<ErrorResponse> handleBindException(BindException ex, WebRequest request) {
//        Map<String, String> errors = new HashMap<>();
//        ex.getBindingResult().getAllErrors().forEach(error -> {
//            String fieldName = ((FieldError) error).getField();
//            String errorMessage = error.getDefaultMessage();
//            errors.put(fieldName, errorMessage);
//        });
//
//        String errorMessage = errors.entrySet().stream()
//                .map(entry -> entry.getKey() + ": " + entry.getValue())
//                .collect(Collectors.joining(", "));
//
//        log.error("Binding error: {}", errorMessage);
//        return createErrorResponse(HttpStatus.BAD_REQUEST, "Binding error", request.getDescription(false), errors);
//    }
//
//    @ExceptionHandler(ConstraintViolationException.class)
//    @ResponseStatus(HttpStatus.BAD_REQUEST)
//    public ResponseEntity<ErrorResponse> handleConstraintViolationException(ConstraintViolationException ex, WebRequest request) {
//        Map<String, String> errors = new HashMap<>();
//        ex.getConstraintViolations().forEach(violation -> {
//            String propertyPath = violation.getPropertyPath().toString();
//            String message = violation.getMessage();
//            errors.put(propertyPath, message);
//        });
//
//        String errorMessage = errors.entrySet().stream()
//                .map(entry -> entry.getKey() + ": " + entry.getValue())
//                .collect(Collectors.joining(", "));
//
//        log.error("Constraint violation: {}", errorMessage);
//        return createErrorResponse(HttpStatus.BAD_REQUEST, "Constraint violation", request.getDescription(false), errors);
//    }
//
//    @ExceptionHandler(BusinessException.class)
//    @ResponseStatus(HttpStatus.BAD_REQUEST)
//    public ResponseEntity<ErrorResponse> handleBusinessException(BusinessException ex, WebRequest request) {
//        log.error("Business exception occurred: {}", ex.getMessage());
//        return createErrorResponse(HttpStatus.BAD_REQUEST, ex.getMessage(), request.getDescription(false));
//    }
//
//    @ExceptionHandler(Exception.class)
//    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
//    public ResponseEntity<ErrorResponse> handleGlobalException(Exception ex, WebRequest request) {
//        log.error("Unhandled exception occurred", ex);
//        return createErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, "An unexpected error occurred", request.getDescription(false));
//    }
//
//    private ResponseEntity<ErrorResponse> createErrorResponse(HttpStatus status, String message, String path) {
//        return createErrorResponse(status, message, path, null);
//    }
//
//    private ResponseEntity<ErrorResponse> createErrorResponse(HttpStatus status, String message, String path, Map<String, String> errors) {
//        ErrorResponse errorResponse = new ErrorResponse(
//                status.value(),
//                status.getReasonPhrase(),
//                message,
//                path.replace("uri=", ""),
//                errors
//        );
//        return new ResponseEntity<>(errorResponse, status);
//    }
//}